<?php

class AjaxcallModule extends fvModule {

    function __construct () {
        $moduleName = strtolower(substr(__CLASS__, 0, -6));

        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showAddtocart() {
    	$count = fvRequest::getInstance()->getRequestParameter("count");
    	$product_id = fvRequest::getInstance()->getRequestParameter("product_id");
    	$cook = new fvCookie();
    	$cook->add("product", array($product_id => $count));
    	$vals = $cook->extract();
    	$count = 0;
    	$summ = 0;
    	if ($vals["product"])
    	foreach ($vals["product"] as $k=>$v){
    		$Product = ProductsManager::getInstance()->getByPk($k);
    		$count += $v;
    		$summ += ($Product->price - $Product->price*$Product->discount/100) * $v;
    	}
    	$this->__assign(array(
            'count' => $count,
        	'price' => number_format($summ, 2, ',', ' ')
        ));
        return $this->__display('ajax_addtocart.tpl');
    }
    
	function showRecalccart() {
    	$count = fvRequest::getInstance()->getRequestParameter("count");
    	$cook = new fvCookie();
		if ($count){
    		foreach ($count as $k=>$v){
    			$cook->put("product", array($k => $v));
    		}
    	}
    	$vals = $cook->extract();
    	$count = 0;
    	$summ = 0;
    	if ($vals["product"])
    	foreach ($vals["product"] as $k=>$v){
    		$Product = ProductsManager::getInstance()->getByPk($k);
    		$count += $v;
    		$summ += ($Product->price - $Product->price*$Product->discount/100) * $v;
    	}
    	$this->__assign(array(
            'count' => $count,
        	'price' => number_format($summ, 2, ',', ' ')
        ));
        return $this->__display('ajax_addtocart.tpl');
    }

}

?>
